<?php

namespace App\Http\Controllers\Admin;

use App\Exports\PassLogExport;
use App\Http\Filters\PassLogFilter;
use App\Models\PassLog;
use App\Http\Requests\PassLogRequest;
use App\Http\Resources\PassLogResource;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class PassLogController extends Controller
{
    public function index(PassLogFilter $filter)
    {
        $passLogs = PassLog::query()
            ->with('user','housings','housingsorder','equipments')
            ->filter($filter)
            ->paginate();

        return $this->ok(PassLogResource::collection($passLogs));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(PassLogRequest $request)
    {
        $inputs = $request->validated();
        $passLog = PassLog::create($inputs);

        return $this->created(PassLogResource::make($passLog));
    }

    public function edit(Request $request, PassLog $passLog)
    {
        return $this->ok(PassLogResource::make($passLog));
    }

    public function update(PassLogRequest $request, PassLog $passLog)
    {
        $inputs = $request->validated();
        $passLog->update($inputs);

        return $this->created(PassLogResource::make($passLog));
    }

    public function destroy(PassLog $passLog)
    {
        $passLog->delete();
        return $this->noContent();
    }

    public function export(Request $request)
    {

        $filters = $request->only([
            'ids',
            // ... 其他可能的过滤条件
        ]);
        return Excel::download(new PassLogExport($filters), '通行记录.xlsx');
    }
}
